<?php

	class Media_ImageHelper
	{
		var $width;
		var $height;
		
		public function __construct($w,$h)
		{
			$this->width = $w;
			$this->height = $h;
		}
		
		public function createthumb($source,$target)
		{
			$ext = EbFilesToolkit::getExtension($source);
			
			if (preg_match("/JPG|JPEG/",$ext)){$src_img=imagecreatefromjpeg($source);}
			elseif (preg_match("/PNG/",$ext)){$src_img=imagecreatefrompng($source);}
			else {return ;}
			
			$old_x=imageSX($src_img);
			$old_y=imageSY($src_img);
			
			if ($old_x > $old_y) 
			{
				$thumb_w=$this->width;
				$thumb_h=$old_y*($this->height/$old_x);
			}
			if ($old_x < $old_y) 
			{
				$thumb_w=$old_x*($this->width/$old_y);
				$thumb_h=$this->height;
			}
			if ($old_x == $old_y) 
			{
				$thumb_w=$this->width;
				$thumb_h=$this->height;
			}
			
			$dst_img=ImageCreateTrueColor($thumb_w,$thumb_h);
			imagecopyresampled($dst_img,$src_img,0,0,0,0,$thumb_w,$thumb_h,$old_x,$old_y); 
			
			if (preg_match("/PNG/",$ext))
			{
				$res = imagepng($dst_img,$target); 
			} 
			else 
			{
				$res = imagejpeg($dst_img,$target); 
			}
			
			imagedestroy($dst_img); 
			imagedestroy($src_img); 
						
			return $res;
		}
		
	}